package com.zhiche.lisa.bms.service.driver.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.mapper.driver.BaseCvTankvolumeMapper;
import com.zhiche.lisa.bms.dao.model.driver.BaseCvTankvolume;
import com.zhiche.lisa.bms.service.driver.IBaseCvTankvolumeService;
import com.zhiche.lisa.bms.pojo.vo.driver.BaseCvQueryConditionsVO;
import com.zhiche.lisa.bms.pojo.vo.driver.BaseCvTankvolumeVO;
import com.zhiche.lisa.core.supports.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品车型油箱容积, cv = commercial vehicle 服务实现类
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@Service
@Slf4j
public class BaseCvTankvolumeServiceImpl extends ServiceImpl<BaseCvTankvolumeMapper, BaseCvTankvolume> implements IBaseCvTankvolumeService {

    /**
     * 分页查询
     *
     * @param pageVO
     * @return
     */
    @Override
    public Page<BaseCvTankvolumeVO> listByPage(Page<BaseCvTankvolumeVO> pageVO) {
        log.info("BaseCvTankvolumeServiceImpl.listByPage params : {}", pageVO);
        if (Objects.isNull(pageVO)) {
            return null;
        }
        pageVO.setOrderByField("gmt_update");
        pageVO.setAsc(false);
        EntityWrapper<BaseCvTankvolumeVO> ew = whereCondition(pageVO);
        List<BaseCvTankvolumeVO> list = baseMapper.selectByConditions(pageVO,ew);
        pageVO.setRecords(list);
        return  pageVO;
    }

    /**
     * 添加
     *
     * @param baseCvTankvolumeVO
     * @param br
     * @return
     */
    @Override
    public Integer add(BaseCvTankvolumeVO baseCvTankvolumeVO, BindingResult br) {
        log.info("BaseCvTankvolumeServiceImpl.add param : {}", baseCvTankvolumeVO);

        if (Objects.isNull(baseCvTankvolumeVO)) {
            log.error("BaseCvTankvolumeServiceImpl.add param form must not be null");
            throw new BaseException("表单不能为空");
        }

        //表单验证
        verificationForm(br);

        //验证车型是否唯一
        this.verificationCarTypeUnique(baseCvTankvolumeVO);

        BaseCvTankvolume baseCvTankvolume = new BaseCvTankvolume();
        BeanUtils.copyProperties(baseCvTankvolumeVO,baseCvTankvolume);
        this.insert(baseCvTankvolume);
        return baseCvTankvolume.getId();
    }

    /**
     * 更新
     *
     * @param baseCvTankvolumeVO
     * @param br
     * @return
     */
    @Override
    public Boolean update(BaseCvTankvolumeVO baseCvTankvolumeVO, BindingResult br) {
        log.info("BaseCvTankvolumeServiceImpl.update param : {}", baseCvTankvolumeVO);

        if (Objects.isNull(baseCvTankvolumeVO)) {
            log.error("BaseCvTankvolumeServiceImpl.update param form must not be null");
            throw new BaseException("车型分类表单不能为空");
        }

        //验证表单
        verificationForm(br);

        //业务验证 车型分类编码唯一
        this.verificationCarTypeUnique(baseCvTankvolumeVO);

        BaseCvTankvolume baseCvTankvolume = new BaseCvTankvolume();
        BeanUtils.copyProperties(baseCvTankvolumeVO, baseCvTankvolume);
        return this.updateById(baseCvTankvolume);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws BaseException
     */
    @Override
    public Boolean del(Integer id) throws BaseException {
        log.info("BaseCvTankvolumeServiceImpl.del param : {}", id);

        if ( Objects.isNull(id) || id.equals(0)  ) {
            log.error("BaseCvTankvolumeServiceImpl.del param id must not be null");
            throw new BaseException("主键不能为空");
        }
        return this.deleteById(id);
    }

    /**
     * 根据条件查询
     *
     * @param baseCvQueryConditionsVO@return
     */
    @Override
    public List<BaseCvTankvolumeVO> selectListByConditions(BaseCvQueryConditionsVO baseCvQueryConditionsVO) {
        log.info("BaseCvTankvolumeServiceImpl.selectListByConditions params : {}", baseCvQueryConditionsVO);
        EntityWrapper<BaseCvTankvolume> ew = new EntityWrapper<>();
        if (StringUtils.isNotBlank(baseCvQueryConditionsVO.getCvTypeId())) {
            ew.eq("cv_type_id", baseCvQueryConditionsVO.getCvTypeId());
        }
        if (Objects.isNull(baseCvQueryConditionsVO.getTankVolume())) {
            ew.eq("tank_volume", baseCvQueryConditionsVO.getTankVolume());
        }
        List<BaseCvTankvolume> list = this.selectList(ew);
        List<BaseCvTankvolumeVO> listVO = Lists.newArrayList();

        list.forEach(baseCvTankvolume -> {
            BaseCvTankvolumeVO vo = new BaseCvTankvolumeVO();
            BeanUtils.copyProperties(baseCvTankvolume,vo);
            listVO.add(vo);
        });

        return  listVO;
    }

    /**
     * 根据ID得到商品车型油箱容积
     *
     * @param id
     * @return
     */
    @Override
    public BaseCvTankvolumeVO getById(Integer id) {
        EntityWrapper<BaseCvTankvolumeVO> ew = new EntityWrapper<>();
        ew.eq("id",id);
        return baseMapper.getOneByEW(ew);
    }

    /**
     * 查询条件
     * @returnVO
     */
    private EntityWrapper<BaseCvTankvolumeVO> whereCondition(Page<BaseCvTankvolumeVO> page){
        EntityWrapper<BaseCvTankvolumeVO> ew = new EntityWrapper<>();

        //条件
        if (Objects.nonNull(page.getCondition())){
            if (Objects.nonNull(page.getCondition().get("cvTypeId")) && StringUtils.isNotEmpty(page.getCondition().get("cvTypeId").toString())) {
                ew.eq("cv_type_id", page.getCondition().get("cvTypeId").toString().trim());
            }
            if (Objects.nonNull(page.getCondition().get("tankVolume")) && StringUtils.isNotEmpty(page.getCondition().get("tankVolume").toString())) {
                ew.eq("tank_volume", page.getCondition().get("tankVolume").toString().trim());
            }
        }
        return ew;
    }

    private void verificationForm(BindingResult br) {
        if (null == br) {
            log.error("BaseCvTankvolumeServiceImpl.verificationForm error br is null");
            throw new BaseException("BindingResult is null");
        }
        if (br.hasErrors()) {
            List<ObjectError> list = br.getAllErrors();
            log.error("BaseCvTankvolumeServiceImpl.verificationForm error : {}", list.get(0).getDefaultMessage());
            throw new BaseException(list.get(0).getDefaultMessage());
        }
    }

    private void verificationCarTypeUnique(BaseCvTankvolumeVO baseCvTankvolumeVO){
        log.info("BaseCvTankvolumeServiceImpl.verificationCarTypeUnique params : {}", baseCvTankvolumeVO);

        if (Objects.isNull(baseCvTankvolumeVO.getCvTypeId())) {
            log.error("BaseCvTankvolumeServiceImpl.verificationCarTypeUnique param form must not be null");
            throw new BaseException("车型不能为空");
        }
        EntityWrapper<BaseCvTankvolume> ew = new EntityWrapper<>();
        ew.eq("cv_type_id", baseCvTankvolumeVO.getCvTypeId());
        List<BaseCvTankvolume> list = this.selectList(ew);

        if (CollectionUtils.isNotEmpty(list)) {
            BaseCvTankvolume baseCvTankvolume = list.get(0);
            //修改时判断 如果id一致则修改时未改变车型
            if (baseCvTankvolumeVO.getId() != null && baseCvTankvolumeVO.getId().equals(baseCvTankvolume.getId())) {
                return;
            }
            log.error("BaseCvTankvolumeServiceImpl.verificationCarTypeUnique error list is not empty");
            throw new BaseException("车型已存在！");
        }
    }
}
